package com.booking.springrestful.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.springrestful.domain.Announcements;
import com.booking.springrestful.mapper.AnnouncementsMapper;
import com.booking.springrestful.service.AnnouncementsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
* @author 13182
* @description 针对表【announcements(公告表)】的数据库操作Service实现
* @createDate 2024-11-19 10:13:43
*/
@Service
public class AnnouncementsServiceImpl extends ServiceImpl<AnnouncementsMapper, Announcements>
    implements AnnouncementsService {

    @Resource
    private AnnouncementsMapper announcementsMapper;

    @Override
    public void announce(Announcements announcement) {
        announcement.setCreatedat(new Date());
        announcementsMapper.insert(announcement);
    }

    @Override
    public List<Announcements> getAnnouncements() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取一周前的时间
        LocalDateTime oneWeekAgo = now.minusDays(7);

        // 构造查询条件
        QueryWrapper<Announcements> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("createdAt", oneWeekAgo);  // 大于等于一周前的时间

        // 查询数据
        return announcementsMapper.selectList(queryWrapper.orderByDesc("createdAt"));
    }
}




